package com.chat.view;

import com.chat.Local;
import com.chat.service.Resetpassword;
import com.chat.utils.Md5;
import com.chat.utils.MouseFollow;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class ResetController  {
      @FXML
      private Button cancelbutton;
      @FXML
      private TextField passwordTextField;
      @FXML
      private TextField confirmTextField;
      @FXML
      private Label passwordlabel;
      @FXML
      private Label confirmlabel;
      @FXML
      private Button resetbutton;
      @FXML
      private TextField mailboxTextField;
      @FXML
      private Button returnbutton;

      public void cancelbuttonOnAction(javafx.event.ActionEvent event) {
            Stage stage=(Stage) cancelbutton.getScene().getWindow();
            stage.close();
      }

      public void resetbuttonOnAction(javafx.event.ActionEvent event){
            if(passwordTextField.getText().isEmpty()==false&&passwordTextField.getText().toString().matches("^[A-Za-z0-9]+${6,15}"))
            {
                    passwordlabel.setText("格式正确！");
            }
            else passwordlabel.setText("格式错误！");
            if(passwordTextField.getText().isEmpty()==false&&passwordTextField.getText().toString().equals(confirmTextField.getText().toString()))
            {
                  confirmlabel.setText("密码重置成功！");
                  validateLogin();
            }
            else confirmlabel.setText("两次密码不一致！");
       }
      public void validateLogin(){
//            DatabaseConnection connectionNow=new DatabaseConnection();
//            Connection conn=connectionNow.getConnection();
//
//            String sql="update user_account set password='"+passwordTextField.getText()+"'where username='"+usernameTextField.getText()+"'";
//            try {
//                  Statement stmt=conn.createStatement();
//                  int count=stmt.executeUpdate(sql);
//                  System.out.println(count);
//
//            } catch (SQLException e) {
//                  e.printStackTrace();
//            }
           String Password;
            Md5 k=new Md5();
            Password=k.digest(passwordTextField.getText());
            Resetpassword.resetpassword(Local.user.getMailbox(),Password);
      }
      public void returnbuttonOnAction(javafx.event.ActionEvent event){
            Stage stage=(Stage) cancelbutton.getScene().getWindow();
            stage.close();
            try {
                  FXMLLoader fxmlLoader = new FXMLLoader();
                  fxmlLoader.setLocation(getClass().getResource("login.fxml"));
                  Parent root = fxmlLoader.load();
                  Stage resetstage=new Stage();
                  Scene scene = new Scene(root, 600, 400);
                  resetstage.initStyle(StageStyle.TRANSPARENT);
                  MouseFollow in = new MouseFollow(root, resetstage);
                  resetstage.setScene(scene);
                  resetstage.show();
            }catch(Exception e){
                  e.printStackTrace();
            }
      }

}
